import isFunction from "./isFunction"
import isFunctionComponent from "./isFunctionComponent"
import mountElement from "./mountElement"
import mountElementNative from "./mountElementNative"

export default function mountComponent (virtualDOM, container) {
  let nextVirtual = null

  if (isFunctionComponent(virtualDOM)) {
    nextVirtual = buildFunctionComponent(virtualDOM)
  } else {
    nextVirtual = buildClassComponent(virtualDOM)
  }

  if (isFunction(nextVirtual)) {
    mountElement(nextVirtual, container)
  } else {
    mountElementNative(nextVirtual, container)
  }
}
  
function buildFunctionComponent (virtualDOM) {
  return virtualDOM.type(virtualDOM.props || {})
}

function buildClassComponent (virtualDOM) {
  const instance = new virtualDOM.type(virtualDOM.props || {})
  const newVirtual = instance.render()
  newVirtual._component = instance
  return newVirtual
}